package com.aleaho.cftrack.presenter;

import android.app.Activity;
import android.util.Log;

import com.aleaho.cftrack.CFTrackApplication;
import com.aleaho.cftrack.model.LocationModel;
import com.aleaho.cftrack.model.LocationModelImpl;
import com.aleaho.cftrack.utils.CommonUtil;
import com.aleaho.cftrack.utils.Constants;
import com.aleaho.cftrack.utils.MapUtil;
import com.aleaho.cftrack.utils.TrackConsts;
import com.aleaho.cftrack.utils.ViewUtil;
import com.aleaho.cftrack.view.LocationView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.trace.api.entity.EntityInfo;
import com.baidu.trace.api.entity.EntityListRequest;
import com.baidu.trace.api.entity.EntityListResponse;
import com.baidu.trace.api.entity.FilterCondition;
import com.baidu.trace.api.entity.OnEntityListener;
import com.baidu.trace.api.track.LatestPoint;
import com.baidu.trace.api.track.LatestPointRequest;
import com.baidu.trace.api.track.LatestPointResponse;
import com.baidu.trace.api.track.OnTrackListener;
import com.baidu.trace.model.ProcessOption;
import com.baidu.trace.model.StatusCodes;

import java.util.List;
import java.util.Locale;

/**
 * @author Aleaho
 * @version 1.0
 * @date 2017/11/2
 * @desc
 */

public class LocationPresenter {
    private LocationView locationView = null;
    private String entity = "";
    private CFTrackApplication trackApp = null;
    private LocationModel locationModel = null;
    private Activity activity= null;


    private OnEntityListener onEntityListener = null;
    private OnTrackListener onTrackListener = null;
    private ViewUtil viewUtil = null;


    public LocationPresenter(CFTrackApplication trackApp, LocationView locationView, Activity activity) {
        this.trackApp = trackApp;
        this.locationView = locationView;
        this.activity = activity;
        locationModel = new LocationModelImpl();
        viewUtil = new ViewUtil();
        initListener();
    }

    private void initListener() {

        onEntityListener = new OnEntityListener() {
            @Override
            public void onEntityListCallback(EntityListResponse response) {
                /**
                 * 返回结果不正确时，直接返回，并提示信息
                 */
                if (StatusCodes.SUCCESS != response.getStatus()) {

                    viewUtil.showToast(activity, response.getMessage());
                    Log.i(Constants.TAG, String.format(Locale.CHINA, "查询结果为： 状态码=%d,信息=%s",
                            response.getStatus(), response.getMessage()));
                    return;
                }
                List<EntityInfo> entities = response.getEntities();
                if (entities.size() < 1){
                    return;
                }

                locationView.showEntities(entities);
            }
        };

        onTrackListener = new OnTrackListener() {
            @Override
            public void onLatestPointCallback(LatestPointResponse response) {
                if (StatusCodes.SUCCESS != response.getStatus()) {
                    return;
                }

                LatestPoint point = response.getLatestPoint();
                if (null == point || CommonUtil.isZeroPoint(point.getLocation().getLatitude(), point.getLocation()
                        .getLongitude())) {
                    return;
                }

                LatLng currentLatLng = MapUtil.convertTrace2Map(point.getLocation());
                if (null == currentLatLng) {
                    return;
                }

               locationView.updateLatestPoint(currentLatLng);
            }
        };


    }


    /**
     * @param entities
     * @param activeTime
     * @param pageIndex
     */
    public void queryEntityList(List<String> entities, long activeTime, int pageIndex) {

        locationModel.queryEntityList(trackApp, entities, activeTime, pageIndex, onEntityListener);

    }

    /**
     * @param entity
     */
    public void queryLatestPoint(String entity) {

        locationModel.queryLatestPoint(trackApp, entity, onTrackListener);
    }


}
